package io.renren.modules.common.controller;

import java.util.*;
import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.common.entity.AreasEntity;
import io.renren.modules.common.entity.CitiesEntity;
import io.renren.modules.common.service.AreasService;
import io.renren.modules.common.service.CitiesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.common.entity.ProvincesEntity;
import io.renren.modules.common.service.ProvincesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 省份信息表
 *
 * @author nyc
 * @email 2691778746@qq.com
 * @date 2022-03-08 17:45:36
 */
@RestController
@RequestMapping("common/provinces")
public class ProvincesController {
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private CitiesService citiesService;
    @Autowired
    private AreasService areasService;





    @PostMapping("/resPCAInfo")
    public R resPCAInfo(@RequestBody String[] ids){
        String provincesCode = ids[0];
        String citiesCode = ids[1];
        String areasCode = ids[2];

        QueryWrapper<ProvincesEntity> provincesWrapper = new QueryWrapper<>();
        QueryWrapper<CitiesEntity> citiesWrapper = new QueryWrapper<>();
        QueryWrapper<AreasEntity> areasWrapper = new QueryWrapper<>();

        provincesWrapper.eq("provinceid",provincesCode);
        citiesWrapper.eq("cityid",citiesCode);
        areasWrapper.eq("areaid",areasCode);


        ProvincesEntity provincesEntity = provincesService.getOne(provincesWrapper);
        CitiesEntity citiesEntity = citiesService.getOne(citiesWrapper);
        AreasEntity areasEntity = areasService.getOne(areasWrapper);

        String[] fullAddress = new String[3];
        fullAddress[0] = provincesEntity.getProvincename();
        fullAddress[1] = citiesEntity.getCityname();
        fullAddress[2] = areasEntity.getAreaname();

        return R.ok().put("data",fullAddress);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("common:provinces:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = provincesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("common:provinces:info")
    public R info(@PathVariable("id") Integer id){
		ProvincesEntity provinces = provincesService.getById(id);

        return R.ok().put("provinces", provinces);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("common:provinces:save")
    public R save(@RequestBody ProvincesEntity provinces){
		provincesService.save(provinces);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("common:provinces:update")
    public R update(@RequestBody ProvincesEntity provinces){
		provincesService.updateById(provinces);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("common:provinces:delete")
    public R delete(@RequestBody Integer[] ids){
		provincesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
